package com.xxgc.zde.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
 * 全局统一结果相应类
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    private int code;
    private String msg;
    private T data;

    // 成功返回结果，无数据
    public static <T> Result<T> success() {
        return new Result<>(200, "成功", null);
    }

    // 成功返回结果，带数据
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "成功", data);
    }

    // 失败返回结果，自定义错误码和错误信息
    public static <T> Result<T> fail(int code, String msg) {
        return new Result<>(code, msg, null);
    }

    // 失败返回结果，默认错误码 500
    public static <T> Result<T> fail(String msg) {
        return new Result<>(500, msg, null);
    }

    // 链式调用设置 code
    public Result<T> code(int code) {
        this.code = code;
        return this;
    }

    // 链式调用设置 msg
    public Result<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    // 链式调用设置 data
    public Result<T> data(T data) {
        this.data = data;
        return this;
    }
}